package org.adorsys.aderp.jpa.impl;

import org.adorsys.aderp.jpa.api.audit.AbstractAuditRecord;
import org.adorsys.aderp.jpa.api.audit.AbstractAuditRecordBuilder;
import org.adorsys.aderp.jpa.api.audit.AuditRecordBuilder;
import org.springframework.beans.factory.BeanFactory;

public abstract class AuditRecordInfoBuilder {

	/**
	 * 
	 * 
	 * @param javaType : Derives the audit record class name from the java type of the entity.
	 * @param beanFactory : To dicover the record builder class
	 * @param arKlass : technical for generics
	 * @param repositoryMap : holds {@link AderpJPARepository} instantiated.
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static <AR extends AbstractAuditRecord> AuditRecordInfo<AR> buildAuditRecordInfo(
			Class<?> javaType, BeanFactory beanFactory, Class<AR> arKlass){

		String auditRecordClassName = javaType.getName()+"AuditRecord";
		Class<AR> auditRecordClass;
		try {
			auditRecordClass = (Class<AR>) javaType.getClassLoader().loadClass(auditRecordClassName);
		} catch (ClassNotFoundException e) {
			return null;
		}

		Class<? extends AuditRecordBuilder> recordBuilderClass = AbstractAuditRecordBuilder.class;
		AuditRecordBuilder auditRecordBuilder = beanFactory.getBean(recordBuilderClass);
		return new AuditRecordInfo(auditRecordClass, auditRecordBuilder, beanFactory);
	}
}
